package com.njust.service.Impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njust.common.Const;
import com.njust.dataobject.*;
import com.njust.enums.FaultType;
import com.njust.form.HeightRequest;
import com.njust.form.TodayQueryForm;
import com.njust.mapper.*;
import com.njust.service.TrainService;
import com.njust.utils.*;
import com.njust.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhujiawei on 2017/12/29.
 */
@Service
@Slf4j
public class TrainServiceImpl implements TrainService {

    @Autowired
    private TrainInfoMapper trainInfoMapper;

    @Autowired
    private TrainDataMapper trainDataMapper;

    @Autowired
    private MotorInfoMapper motorInfoMapper;

    @Autowired
    private GearInfoMapper gearInfoMapper;

    @Autowired
    private TrainParamMapper trainParamMapper;

    @Autowired
    private TrainFaultMapper trainFaultMapper;

    /**
     * 实时请求 返回列车的状态
     *
     * @param pre 返回当天所有数据
     * @return
     * @throws ParseException
     */
    @Override
    public ResultVO realShowlast(String pre) throws ParseException {
        List<IndexVO> trainInfos = new ArrayList<>();
        TrainInfoVO trainInfoVO = new TrainInfoVO();
        Date receivetime = DateUtil.String2Date(pre);
        long time1 = receivetime.getTime()+12600000;//从早三点半开始计时
        Date firsttime = new Date(time1);
        long time = firsttime.getTime() + 86400000;
        Date secondtime = new Date(time);

        //查询到数据库最后一条数据train_onlyid
        TrainInfo trainInfo = trainInfoMapper.findTrainInfoByLastTrainId();
        if (trainInfo == null) {
            return ResultVO.createByErrorMessage("无数据");
        }
        TrainInfoExample trainInfoExample = new TrainInfoExample();
        TrainInfoExample.Criteria criteria = trainInfoExample.createCriteria();

        if (trainInfo.getTrainId() != null) {
            trainInfoVO.setTrainId(trainInfo.getTrainId());
            criteria.andTrainIdEqualTo(trainInfo.getTrainId());
            criteria.andTrainDateBetween(firsttime, secondtime);//1天
            trainInfoExample.setOrderByClause("train_onlyid DESC");
        }
        //根据车牌查询出当天的所有这个车牌号的数据
        List<TrainInfo> trainInfoList = trainInfoMapper.selectByExample(trainInfoExample);
        if (trainInfoList.size() == 0) {
            return ResultVO.createByErrorMessage("无数据");
        }
        for (TrainInfo trainInfo1 : trainInfoList) {
            //返回时需要添加 时间 解决悬浮显示时间的需求
            IndexVO indexVO = new IndexVO();
            indexVO.setTrainOnlyid(trainInfo1.getTrainOnlyid());
            indexVO.setTrainState(trainInfo1.getTrainState());
            indexVO.setTrainDirection(trainInfo1.getTrainDirection());
            indexVO.setTrainDate(DateUtil.Date2String(trainInfo1.getTrainDate()));
            trainInfos.add(indexVO);
        }
        //测试用
        //List<TrainInfo> trainInfos1 = trainInfos.subList(1, 4);

        if (trainInfos.size() >= 23) {
            List<IndexVO> trainInfos1 = trainInfos.subList(1, 15);
            trainInfoVO.setTrainInfoList(trainInfos1);
            return ResultVO.createBySuccess(trainInfoVO);
        }
        trainInfoVO.setTrainInfoList(trainInfos);
        return ResultVO.createBySuccess(trainInfoVO);//1天该躺车的所有数据
    }

    /**
     * @param pre
     * @return
     * @throws ParseException
     */
    @Override
    public ResultVO realShowall(String pre) throws ParseException {

        Map<String, List<IndexVO>> traininfoMap = new HashMap<>();
        List<String> trainIdList = Arrays.asList(Const.TRAINID);
        Date receivetime = DateUtil.String2Date(pre);
        //同样的进行处理
        long time1 = receivetime.getTime()+12600000;
        Date firsttime = new Date(time1);
        long time = firsttime.getTime() + 86400000;
        Date secondtime = new Date(time);

        for (String trainId : trainIdList) {
            List<IndexVO> trainInfos = new ArrayList<>();

            TrainInfoExample trainInfoExample = new TrainInfoExample();
            TrainInfoExample.Criteria criteria = trainInfoExample.createCriteria();
            criteria.andTrainIdEqualTo(trainId);
            criteria.andTrainDateBetween(firsttime, secondtime);
            trainInfoExample.setOrderByClause("train_date DESC");

            List<TrainInfo> trainInfos1 = trainInfoMapper.selectByExample(trainInfoExample);
            if (trainInfos1.size() == 0) {
                continue;
            }

            for (TrainInfo tf : trainInfos1) {
                //返回时需要添加时间解决悬浮显示时间的需求
                IndexVO indexVO = new IndexVO();
                indexVO.setTrainOnlyid(tf.getTrainOnlyid());
                indexVO.setTrainState(tf.getTrainState());
                indexVO.setTrainDirection(tf.getTrainDirection());
                indexVO.setTrainDate(DateUtil.Date2String(tf.getTrainDate()));
                trainInfos.add(indexVO);
            }
            traininfoMap.put(trainId, trainInfos);
        }
        if (traininfoMap.entrySet().size() == 0) {
            return ResultVO.createByErrorMessage("无数据");
        }
        return ResultVO.createBySuccess(traininfoMap);
    }

    @Override
    public ResultVO warn(Long trainOnlyid) throws IOException {
        List<TrainFaultVO> trainFaultVOList = new ArrayList<>();
        TrainFaultExample trainFaultExample = new TrainFaultExample();
        TrainFaultExample.Criteria criteria = trainFaultExample.createCriteria();

        if (trainOnlyid != null) {
            criteria.andTrainOnlyidEqualTo(trainOnlyid);
        } else {
            return ResultVO.createByErrorMessage("无数据");
        }

        List<TrainFault> trainFaultList = trainFaultMapper.selectByExample(trainFaultExample);
        if (trainFaultList.size() == 0) {
            return ResultVO.createByErrorMessage("无数据");
        }
        for (TrainFault trainFault : trainFaultList) {
            TrainFaultVO trainFaultVO = new TrainFaultVO();
            trainFaultVO.setTrainOnlyid(trainFault.getTrainOnlyid());

            trainFaultVO.setFaultType(trainFault.getFaultType());
            int type=trainFault.getFaultType();

            switch (type){
                case 1:
                    trainFaultVO.setFaultMessage(FaultType.YCALARM.getMsg());
                    break;
                case 2:
                    trainFaultVO.setFaultMessage(FaultType.GAPALARM.getMsg());
                    break;
                case 3:
                    trainFaultVO.setFaultMessage(FaultType.SLOTDALARM.getMsg());
                    break;
                case 4:
                    trainFaultVO.setFaultMessage(FaultType.SLOTHALARM.getMsg());
                    break;
                case 5:
                    trainFaultVO.setFaultMessage(FaultType.TEMPALARM.getMsg());
                    break;
                case 6:
                    trainFaultVO.setFaultMessage(FaultType.YCWARN.getMsg());
                    break;
                case 7:
                    trainFaultVO.setFaultMessage(FaultType.GAPWARN.getMsg());
                    break;
                case 8:
                    trainFaultVO.setFaultMessage(FaultType.SLOTDWARN.getMsg());
                    break;
                case 9:
                    trainFaultVO.setFaultMessage(FaultType.SLOTHWARN.getMsg());
                    break;
                case 10:
                    trainFaultVO.setFaultMessage(FaultType.TEMPWARN.getMsg());
                    break;
                /**
                 * 添加两条排障器高度预警和排障器高度报警
                  */
                case 11:
                    trainFaultVO.setFaultMessage(FaultType.PILOTWARM.getMsg());
                    break;
                case 12:
                    trainFaultVO.setFaultMessage(FaultType.PILOTALARM.getMsg());
                    break;

                default: break;
            }
            if(trainFaultVO.getFaultMessage()==null){
                return ResultVO.createByErrorMessage("无数据");
            }
            trainFaultVO.setTrainDate(DateUtil.Date2String(trainFault.getTrainDate()));
            trainFaultVOList.add(trainFaultVO);
        }
        return ResultVO.createBySuccess(trainFaultVOList);
    }

    /**
     * 1 报警 2是预警 0表示恢复
     *
     * @param warnRank
     * @return
     * @throws IOException
     */
    @Override
    public ResultVO deviceState(Integer warnRank) throws IOException {
        TcpUtil.action(warnRank);
        return ResultVO.createBySuccess();
    }



    /**
     * 根据列车记录编号查询出 列车的综合信息
     *
     * @param trainOnlyid
     * @return
     */
    @Override
    public ResultVO findTrainInfoByTrainOnlyid(Long trainOnlyid) {
        TrainDataVO trainDataVO = this.findByTrainOnlyid(trainOnlyid);
        if (trainDataVO == null) {
            return ResultVO.createByErrorMessage("无数据");
        }
        return ResultVO.createBySuccess(trainDataVO);
    }

    /**
     * 根据时间 返回当日的行车数据 形成分页报表每页显示26条  已经完成分页功能
     *
     * @param pre
     * @param after
     * @return
     */
    @Override
    public ResultVO findByTrainDate(Date pre, Date after, Integer page, Integer size) {

        //返回的值
        List<TrainInfo> trainInfos = trainInfoMapper.findByStateandTrainDate(pre, after);
        if(trainInfos.size()==0){
            return ResultVO.createByErrorMessage("无数据");
        }

        //唯一编号
        List<Long> trainOnlyIdList=new ArrayList<>();
        //拿到唯一编号
        for (TrainInfo trainInfo:trainInfos){
           trainOnlyIdList.add(trainInfo.getTrainOnlyid());
        }

        if(trainOnlyIdList.size()==0){
            return ResultVO.createByErrorMessage("无数据");
        }
        //trainData查询条件
        TrainDataExample trainDataExample=new TrainDataExample();
        TrainDataExample.Criteria criteria = trainDataExample.createCriteria();
         //新添加的进行逆序排列
        trainDataExample.setOrderByClause("train_date DESC");

        criteria.andTrainOnlyidIn(trainOnlyIdList);
        //设置分页信息
        List<TrainData> traindatas= new ArrayList<>();

        PageHelper.startPage(page,size);
        //根据条件进行查询trainData信息
        traindatas=trainDataMapper.selectByExample(trainDataExample);
        PageInfo<TrainData> pageInfo  = new PageInfo<>(traindatas);

        if (traindatas.size() == 0) {
            return ResultVO.createByErrorMessage("无数据");
        }

        //返回的值
        List<TrainDataVO> trainDataVOList = new ArrayList<>();

        for (TrainData trainData : traindatas) {
            TrainDataVO trainDataVO = this.findByTrainOnlyid(trainData.getTrainOnlyid());
            if (trainDataVO == null) {
                log.info("trainDataVO存在NULL");
            } else {
                trainDataVOList.add(trainDataVO);
            }
        }
        System.out.println(trainDataVOList);
        PageResultVo pageResultVo = new PageResultVo();
        PageUtils.pageUtil(pageInfo, pageResultVo);
        pageResultVo.setList(trainDataVOList);
        return ResultVO.createBySuccess(pageResultVo);
    }

    /**
     * 当日运营结束返回 定时返回的数据 3和78齿
     *
     * @param pre
     * @param after
     * @return
     */
    @Override
    public ResultVO todayLast10(Date pre, Date after) {
        //存储数据
        List<QixiMinVO> qixiMinVOList = new ArrayList<>();

        //根据排序查找到当日车气隙最下的10条记录
        TrainDataExample trainDataExample = new TrainDataExample();
        TrainDataExample.Criteria criteria1 = trainDataExample.createCriteria();
        criteria1.andTrainDateBetween(pre, after);
        trainDataExample.setOrderByClause("gapmin_val ASC");
        List<TrainData> trainDataList = trainDataMapper.selectByExample(trainDataExample);
        for (TrainData trainData : trainDataList) {
            QixiMinVO qixiMinVO = new QixiMinVO();
            qixiMinVO.setGapminVal(trainData.getGapminVal());
            qixiMinVO.setMotorNum(trainData.getGapMin());
            qixiMinVO.setTrainOnlyid(trainData.getTrainOnlyid());

            TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(trainData.getTrainOnlyid());
            if (trainInfo == null) {
                continue;
            }
            BeanUtils.copyProperties(trainInfo, qixiMinVO);
            qixiMinVO.setTrainDate(DateUtil.Date2String(trainInfo.getTrainDate()));

            MotorInfo motorInfo = motorInfoMapper.findByTrainIdAndMotorNum(trainData.getTrainOnlyid(), trainData.getGapMin());
            if (motorInfo.getMotorId() == null) {
                continue;
            }
            qixiMinVO.setTempMax(motorInfo.getTempMax());
            qixiMinVO.setTempMin(motorInfo.getTempMin());
            //查 3和78齿的数据
            GearInfo three = gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(), 3);
            qixiMinVO.setTlgapMin(three.getLgapValue());
            qixiMinVO.setTrgapMin(three.getRgapValue());
            GearInfo seven = gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(), 78);
            qixiMinVO.setSlgapMin(seven.getLgapValue());
            qixiMinVO.setSrgapMin(seven.getRgapValue());
            //封装list
            qixiMinVOList.add(qixiMinVO);
        }
        //筛选出最小的十条记录
        List<QixiMinVO> result = new ArrayList<>();
        Integer size = qixiMinVOList.size();
        if (size >= 10) {
            result = qixiMinVOList.subList(0, 10);
        } else {
            result = qixiMinVOList.subList(0, size);
        }
        return ResultVO.createBySuccess(result);
    }

    /**
     * 当日列车气隙数据低于某一个值
     *
     * @param todayQueryForm
     * @return
     * @throws ParseException
     */
    @Override
    public ResultVO TodayQuery(TodayQueryForm todayQueryForm) throws ParseException {
        List<TodayQueryVO> todayQueryVOList = new ArrayList<>();
        //时间获取
        String trainDate = todayQueryForm.getTrainDate().substring(0, 8) + "000000";
        Date firsttime = DateUtil.String2Date(trainDate);
        long time = firsttime.getTime() + 86400000;
        Date secondtime = new Date(time);

        //根据时间查询出主表的信息
        PageHelper.startPage(todayQueryForm.getPage(), todayQueryForm.getPageSize());
        List<TrainInfo> trainInfoList = trainInfoMapper.findTrainInfoByDatetime(firsttime, secondtime);
        if (trainInfoList.size() == 0) {
            return ResultVO.createByErrorMessage("无数据");
        }
        PageInfo<TrainInfo> pageInfo = new PageInfo<>(trainInfoList);
        for (TrainInfo trainInfo : trainInfoList) {
            TodayQueryVO todayQueryVO = new TodayQueryVO();
            List<TodayQueryMotorVO> todayQueryMotorVOList = new ArrayList<>();
            List<MotorInfo> motorInfoList = motorInfoMapper
                    .findByTrainIdOrderByMotorNum(trainInfo.getTrainOnlyid());
            if (motorInfoList.size() == 0) {
                return ResultVO.createByErrorMessage("无数据");
            }
            BeanUtils.copyProperties(trainInfo, todayQueryVO);
            todayQueryVO.setTrainDate(DateUtil.Date2String(trainInfo.getTrainDate()));

            for (MotorInfo motorInfo : motorInfoList) {

                if (motorInfo.getMotorId() == null) {
                    return ResultVO.createByErrorMessage("无数据");
                }
                List<GearInfo> gearInfoList = gearInfoMapper.findByMotorIdAndInGearNum(motorInfo.getMotorId()
                        , todayQueryForm.getFirstGap(), todayQueryForm.getSecondGap());
                if (gearInfoList.size() == 0) {
                    return ResultVO.createByErrorMessage("无数据");
                }
                for (GearInfo gearInfo : gearInfoList) {
                    if (gearInfo.getRgapValue() < todayQueryForm.getGapValue()
                            || gearInfo.getLgapValue() < todayQueryForm.getGapValue()) {
                        TodayQueryMotorVO todayQueryMotorVO = new TodayQueryMotorVO();
                        BeanUtils.copyProperties(gearInfo, todayQueryMotorVO);
                        todayQueryMotorVO.setMotorNum(motorInfo.getMotorNum());
                        todayQueryMotorVOList.add(todayQueryMotorVO);
                    }
                }
            }
            todayQueryVO.setDataList(todayQueryMotorVOList);
            todayQueryVOList.add(todayQueryVO);
        }
        PageResultVo pageResultVo = new PageResultVo();
        PageUtils.pageUtil(pageInfo, pageResultVo);
        pageResultVo.setList(todayQueryVOList);

        return ResultVO.createBySuccess(pageResultVo);
    }

    /**
     * 分装的函数 根据列车记录的ID查询到列车的VO信息
     *
     * @param trainOnlyid
     * @return
     */
    @Override
    public TrainDataVO findByTrainOnlyid(Long trainOnlyid) {

        TrainDataVO trainDataVO = new TrainDataVO();
        TrainData trainData = trainDataMapper.selectByPrimaryKey(trainOnlyid);
        TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(trainOnlyid);
        if (trainInfo == null || trainData == null) {
            log.info("traininfo无数据");
            return null;
        }
        //读取列车气隙阈值
        TrainParam trainParam = trainParamMapper.findByUdFlag(trainInfo.getTrainDirection());

        //直系属性进行复制
        BeanUtils.copyProperties(trainData, trainDataVO);
        BeanUtils.copyProperties(trainInfo, trainDataVO);
        //设置颜色问题
        if(trainDataVO.getLpilotMin()!=null){
            //进行相应颜色的设置
           Integer result = CompareUtil.compare(Const.PILOT,trainParam.getPilotWarm(),trainParam.getPilotAlarm(),trainDataVO.getLpilotMin());
        trainDataVO.setLpilotMinStatus(result);
        }

        if(trainDataVO.getRpilotMin()!=null){
            //进行相应颜色的设置
            Integer result = CompareUtil.compare(Const.PILOT,trainParam.getPilotWarm(),trainParam.getPilotAlarm(),trainDataVO.getRpilotMin());
            trainDataVO.setRpilotMinStatus(result);
        }

        trainDataVO.setTrainDate(DateUtil.Date2String(trainInfo.getTrainDate()));

        //优化代码 遍历电机号(1-8)
        List<MotorInfo> motorInfoList = motorInfoMapper.findByTrainIdOrderByMotorNum(trainOnlyid);
        if (motorInfoList == null) {
            log.info("电机无数据");
            return null;
        }
        //assembleGearData
        assembleGearData(motorInfoList, trainParam, trainData, trainDataVO);
        return trainDataVO;
    }


    @Override
    //封装一个通用的
    public void assembleGearData(List<MotorInfo> motorInfoList, TrainParam trainParam, TrainData trainData, TrainDataVO trainDataVO) {
        for (MotorInfo motorInfo : motorInfoList) {
            if (motorInfo.getMotorNum() == null) {
                log.info("查不到该电机的详细数据");
                trainDataVO.setLgapMin("无数据");
                trainDataVO.setRgapMin("无数据");
                trainDataVO.setLslotMin("无数据");
                trainDataVO.setRslotMin("无数据");
                trainDataVO.setTempMax("无数据");
                trainDataVO.setTempMax("无数据");
                trainDataVO.setRslothMax("无数据");
                trainDataVO.setLslothMax("无数据");
            } else {
                //如果存在相同的情况 那么会出现无数据  修改代码  通过trainOnlyId 和电机号 得到motor_id
                if (motorInfo.getMotorNum().equals(trainData.getLgapMin())) {
                    //左气隙最小值
                    GearInfo LgearInfo1 = gearInfoMapper
                            .findByMotorIdAndGearNum(motorInfo.getMotorId(), motorInfo.getLgapMin());
                    if (LgearInfo1 == null) {
                        trainDataVO.setLgapMin("无数据");
                    } else {
                        trainDataVO.setLgapMin(StringUtil.changeStr(LgearInfo1.getLgapValue(), trainData.getLgapMin()));
                        if (trainParam != null) {
                            Integer result = CompareUtil.compare(Const.GEAR, trainParam.getGapWarn()
                                    , trainParam.getGapAlarm(), LgearInfo1.getLgapValue());
                            trainDataVO.setLgapStatus(result);
                        }
                    }
                }

                if (motorInfo.getMotorNum().equals(trainData.getRgapMin())) {
                    GearInfo RgearInfo1 = gearInfoMapper
                            .findByMotorIdAndGearNum(motorInfo.getMotorId(), motorInfo.getRgapMin());
                    if (RgearInfo1 == null) {
                        trainDataVO.setRgapMin("无数据");
                    } else {
                        trainDataVO.setRgapMin(StringUtil.changeStr(RgearInfo1.getRgapValue(), trainData.getRgapMin()));
                        if (trainParam != null) {
                            //如果返回的是1 那么就预警 2为报警
                            Integer result = CompareUtil.compare(Const.GEAR, trainParam.getGapWarn()
                                    , trainParam.getGapAlarm(), RgearInfo1.getRgapValue());
                            trainDataVO.setRgapStatus(result);
                        }
                    }
                }
                //槽深最小的数据处理
                if (motorInfo.getMotorNum().equals(trainData.getLslotMin())) {
                    GearInfo LslotInfo = gearInfoMapper
                            .findByMotorIdAndGearNum(motorInfo.getMotorId(), motorInfo.getLslotMin());
                    if (LslotInfo == null) {
                        trainDataVO.setLslotMin("无数据");
                    } else {
                        trainDataVO.setLslotMin(StringUtil.changeStr(LslotInfo.getLslotDepth(), trainData.getLslotMin()));
                        if (trainParam != null) {
                            Integer result = CompareUtil.compare(Const.SLOTDEPTH, trainParam.getSlotdWarn()
                                    , trainParam.getSlotdAlarm(), LslotInfo.getLslotDepth());
                            if(result==0){
                                result = CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),LslotInfo.getLslotDepth());
                            }
                            trainDataVO.setLslotStatus(result);
                        }
                    }
                }
                if (motorInfo.getMotorNum().equals(trainData.getLslotMax())) {
                    GearInfo LslotInfo1 = gearInfoMapper
                            .findByMotorIdAndGearNum(motorInfo.getMotorId(), motorInfo.getLslotMax());
                    if (LslotInfo1 == null) {
                        trainDataVO.setLslotMax("无数据");
                    } else {
                        trainDataVO.setLslotMax(StringUtil.changeStr(LslotInfo1.getLslotDepth(), trainData.getLslotMax()));
                        if (trainParam != null) {
                             Integer   result = CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),LslotInfo1.getLslotDepth());
                            trainDataVO.setLslotMaxStatus(result);
                        }
                    }
                }

                if (motorInfo.getMotorNum().equals(trainData.getRslotMax())) {
                    GearInfo RslotInfo1 = gearInfoMapper
                            .findByMotorIdAndGearNum(motorInfo.getMotorId(), motorInfo.getRslotMax());
                    if (RslotInfo1 == null) {
                        trainDataVO.setRslotMax("无数据");
                    } else {
                        trainDataVO.setRslotMax(StringUtil.changeStr(RslotInfo1.getLslotDepth(), trainData.getLslotMax()));
                        if (trainParam != null) {
                            //都还需要进行添加，更严谨的是上下限槽深预警报警都需要进行比较,以后在进行更改
                            Integer   result = CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),RslotInfo1.getRslotDepth());
                            trainDataVO.setRslotMaxStatus(result);
                        }
                    }
                }

                if (motorInfo.getMotorNum().equals(trainData.getRslotMin())) {
                    GearInfo RslotInfo = gearInfoMapper
                            .findByMotorIdAndGearNum(motorInfo.getMotorId(), motorInfo.getRslotMin());
                    if (RslotInfo == null) {
                        trainDataVO.setRslotMin("无数据");
                    } else {
                        trainDataVO.setRslotMin(StringUtil.changeStr(RslotInfo.getRslotDepth(), trainData.getRslotMin()));
                        if (trainParam != null) {
                            Integer result = CompareUtil.compare(Const.SLOTDEPTH, trainParam.getSlotdWarn()
                                    , trainParam.getSlotdAlarm(), RslotInfo.getRslotDepth());
                            if(result==0){
                                result = CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),RslotInfo.getRslotDepth());
                            }
                            trainDataVO.setRslotStatus(result);
                        }
                    }
                }

                if (motorInfo.getMotorNum().equals(trainData.getLslothMax())) {
                    GearInfo LslothMaxInfo = gearInfoMapper
                            .findByMotorIdAndGearNum(motorInfo.getMotorId(), motorInfo.getLslothMax());
                    if (LslothMaxInfo == null) {
                        trainDataVO.setLslothMax("无数据");
                    } else {
                        trainDataVO.setLslothMax(StringUtil.changeStr(LslothMaxInfo.getLslotValue(), trainData.getLslothMax
                                ()));
                        if (trainParam != null) {
                            //如果返回的是1 那么就预警 2为报警
                            Integer result = CompareUtil.compare(Const.SLOT, trainParam.getSlotWarn()
                                    , trainParam.getSlotAlarm(), LslothMaxInfo.getLslotValue());
                            trainDataVO.setLslothMaxStatus(result);
                        }
                    }
                }
                    //右槽楔数据处理
                if (motorInfo.getMotorNum().equals(trainData.getRslothMax())) {
                    GearInfo RslothMaxInfo = gearInfoMapper
                            .findByMotorIdAndGearNum(motorInfo.getMotorId(), motorInfo.getRslothMax());
                    if (RslothMaxInfo == null) {
                        trainDataVO.setRslothMax("无数据");
                    } else {
                        trainDataVO.setRslothMax(StringUtil.changeStr(RslothMaxInfo.getRslotValue(), trainData.getRslothMax()));
                        if (trainParam != null) {
                            Integer result = CompareUtil.compare(Const.SLOT, trainParam.getSlotWarn()
                                    , trainParam.getSlotAlarm(), RslothMaxInfo.getRslotValue());
                            trainDataVO.setRslothMaxStatus(result);
                        }
                    }
                }

                 if (motorInfo.getMotorNum().equals(trainData.getTempMax())) {
                     if (motorInfo.getTempMax() == null) {
                                trainDataVO.setTempMax("无数据");
                            } else {
                                trainDataVO.setTempMax(StringUtil.changeStr(motorInfo.getTempMax(), trainData.getTempMax()));
                                if (trainParam != null) {

                                    Integer result = CompareUtil.compare(Const.TEMP, trainParam.getTempWarn()
                                            , trainParam.getTempAlarm(), motorInfo.getTempMax());
                                    trainDataVO.setTempMaxStatus(result);
                            }
                     }
                }


            }
        }
    }

    @Override
    public ResultVO heightDifference(HeightRequest heightRequest) throws ParseException {
        HeightDifferenceVo heightDifferenceVo = new HeightDifferenceVo();
        heightDifferenceVo.setIsPop(new Integer(0));
       TrainInfo trainInfo = trainInfoMapper.findTrainInfo();
        List<TrainInfo> trainInfos = trainInfoMapper.findTrainInfoByTrainIdAndDirection(trainInfo.getTrainId(),trainInfo.getTrainDirection());
        if(trainInfos.size()<2){
            return ResultVO.createByErrorMessage("无数据");
        }
        List<Integer> motorIds = Arrays.asList(Const.MONTORID);
        List<MotorBo> motorBos = new ArrayList<>();
        for (Integer motorId:motorIds) {
            MotorBo motorBo = new MotorBo();
            MotorInfo firstMotor = motorInfoMapper.findByTrainIdAndMotorNum(trainInfos.get(0).getTrainOnlyid(),motorId);
            MotorInfo secondMotor = motorInfoMapper.findByTrainIdAndMotorNum(trainInfos.get(1).getTrainOnlyid(),motorId );
            if(firstMotor==null||secondMotor==null){
                return ResultVO.createByErrorMessage("无数据");
            }
            List<Integer> gearNums = Arrays.asList(Const.gearNum);
            List<GearHeightVo> gearHeightVoList = new ArrayList<>();
            for (Integer gearNum:  gearNums) {
                GearHeightVo gearHeightVo = new GearHeightVo();
                gearHeightVo.setLDiff(BigDecimalUtil.dUtil(Math.abs(gearInfoMapper.findLSlotValueByMotorIdAndGearNum(firstMotor.getMotorId(), gearNum)-
                        gearInfoMapper.findLSlotValueByMotorIdAndGearNum(secondMotor.getMotorId(), gearNum)),2));
                gearHeightVo.setRDiff(BigDecimalUtil.dUtil(Math.abs(gearInfoMapper.findRSlotValueByMotorIdAndGearNum(firstMotor.getMotorId(), gearNum)-
                        gearInfoMapper.findRSlotValueByMotorIdAndGearNum(secondMotor.getMotorId(), gearNum)),2));

                //前面的大返回整数1，
                gearHeightVo.setLDiffState(gearHeightVo.getLDiff().compareTo(heightRequest.getWarmValue()));

                gearHeightVo.setRDiffState(gearHeightVo.getRDiff().compareTo(heightRequest.getWarmValue()));

                gearHeightVo.setGearNum(gearNum);
                if(gearHeightVo.getLDiffState()==1||gearHeightVo.getRDiffState()==1){
                    heightDifferenceVo.setIsPop(new Integer(1));
                    gearHeightVoList.add(gearHeightVo);
                }
            }
            if(gearHeightVoList!=null&&gearHeightVoList.size()!=0){
                motorBo.setGearHeightVos(gearHeightVoList);
                motorBo.setMotorNum(motorId);
                motorBos.add(motorBo);
            }else{
                continue;
            }

        }
        heightDifferenceVo.setMotorBos(motorBos);
        BeanUtils.copyProperties(trainInfos.get(0),heightDifferenceVo);
        return ResultVO.createBySuccess(heightDifferenceVo);
    }


    public ResultVO findHeightDiffer(HeightRequest heightRequest) throws ParseException {
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd000000");
      /*  String te = format.format(now);
        Date firstTime = DateUtil.String2Date();
        long time = firstTime.getTime() + 86400000;
        Date lastTime = new Date(time);//当天的数据*/
        Date firstTime = DateUtil.String2Date(heightRequest.getStartTime());
        Date lastTime = DateUtil.String2Date(heightRequest.getEndTime());
        List<HeightVo> heightVoList = new ArrayList<>();
        List<TrainInfo> trainInfos = trainInfoMapper.findByTrainDateAndDirection(firstTime,lastTime,heightRequest.getTrainId(),
                heightRequest.getTrainDirection());
        if(trainInfos.size()<2){
            return ResultVO.createByErrorMessage("无数据");
        }
        for (int i = 0, j = i + 1; j < trainInfos.size(); i++, j++) {
            List<Integer> MotorNums = Arrays.asList(Const.MONTORID);
            for (Integer motorNum : MotorNums) {
                MotorInfo firstMotorInfo = motorInfoMapper.findByTrainIdAndMotorNum(trainInfos.get(i).getTrainOnlyid(), motorNum);
                MotorInfo secondMotorInfo = motorInfoMapper.findByTrainIdAndMotorNum(trainInfos.get(j).getTrainOnlyid(), motorNum);
                if (firstMotorInfo == null || secondMotorInfo == null) {
                    return ResultVO.createByErrorMessage("无数据");
                }

                List<Integer> gearNums = Arrays.asList(Const.gearNum);
                for (Integer gearNum : gearNums) {
                    HeightVo heightVo = new HeightVo();
                    Double a = gearInfoMapper.findLSlotValueByMotorIdAndGearNum(firstMotorInfo.getMotorId(), gearNum);
                    Double c = gearInfoMapper.findLSlotValueByMotorIdAndGearNum(secondMotorInfo.getMotorId(), gearNum);
                   if (a!=null&&c!=null) {
                       heightVo.setLDiff(BigDecimalUtil.dUtil(Math.abs(a -
                               c), 2));
                       heightVo.setRDiff(BigDecimalUtil.dUtil(Math.abs(a -
                              c), 2));
                   }
                    if (heightRequest.getWarmValue()!=null&&heightVo.getLDiff().compareTo(heightRequest.getWarmValue()) > 0) {
                       heightVo.setLDiffState(new Integer(1));
                    } else {
                        heightVo.setLDiffState(new Integer(0));
                    }
                    if (heightRequest.getWarmValue()!=null&&heightVo.getRDiff().compareTo(heightRequest.getWarmValue()) > 0) {
                       heightVo.setRDiffState(new Integer(1));
                    } else {
                        heightVo.setRDiffState(new Integer(0));
                    }
                    heightVo.setGearNum(gearNum);
                    heightVo.setMotorNum(motorNum);
                    BeanUtils.copyProperties(trainInfos.get(i), heightVo);
                    heightVo.setTrainDate(DateUtil.Date2String(trainInfos.get(i).getTrainDate()));
                    heightVoList.add(heightVo);
                }
            }

        }
        heightRequest.setPageSize(20);
        heightRequest.setPage(1);

        Paging paging = Paging.pagination(heightVoList.size(),heightRequest.getPageSize(),heightRequest.getPage());
        int fromIndex = paging.getQueryIndex();
        int toIndex = 0;
        if (fromIndex + paging.getPageSize() >= heightVoList.size()){
            toIndex = heightVoList.size();
        }else {
            toIndex = fromIndex +  paging.getPageSize();
        }
        if (fromIndex > toIndex){
            return ResultVO.createBySuccess(Collections.EMPTY_LIST);
        }
        System.out.println(heightVoList);
        return ResultVO.createBySuccess(heightVoList.subList(fromIndex,toIndex));
    }


}
